﻿// The project released under MS-PL license https://daxeditor.codeplex.com/license

using System.Linq;
using System.Xml.Linq;
using DaxEditor;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace DaxEditorSample.Test
{
    [TestClass]
    public class MeasuresContainerTests
    {
        [TestMethod]
        public void MeasuresFormats()
        {
            string input = Utils.ReadFileFromResources("MeasuresFormats.bim");
            var bim = MeasuresContainer.ParseXmlaAsText(input);
            Assert.IsNotNull(bim.Measures);
            Assert.AreEqual(8, bim.Measures.Count);
            var daxText = bim.GetDaxText();
            Assert.IsNotNull(daxText);

            var measuresFromDax = MeasuresContainer.ParseDaxScript(daxText);
            Assert.AreEqual(bim.Measures.Count, measuresFromDax.Measures.Count);

            var expected = input;
            var actual = measuresFromDax.UpdateMeasuresInXmla(input);
            WindiffAssert.AreEqualNormalizedXmla(expected, actual);
        }

        [TestMethod]
        public void Bim1100()
        {
            string input = Utils.ReadFileFromResources("BIM1100.bim");
            var bim = MeasuresContainer.ParseXmlaAsText(input);
            Assert.IsNotNull(bim.Measures);
            Assert.AreEqual(3, bim.Measures.Count);
            var daxText = bim.GetDaxText();
            Assert.IsNotNull(daxText);

            var measuresFromDax = MeasuresContainer.ParseDaxScript(daxText);
            Assert.AreEqual(bim.Measures.Count, measuresFromDax.Measures.Count);

            var expected = input;
            var actual = measuresFromDax.UpdateMeasuresInXmla(input);
            WindiffAssert.AreEqualNormalizedXmla(expected, actual);
        }

        [TestMethod]
        public void M1()
        {
            try
            {
                string input = Utils.ReadFileFromResources("M1.bim");
                var bim = MeasuresContainer.ParseXmlaAsText(input);
                Assert.IsNotNull(bim.Measures);
                Assert.AreEqual(88, bim.Measures.Count);
                var daxText = bim.GetDaxText();
                Assert.IsNotNull(daxText);

                var measuresFromDax = MeasuresContainer.ParseDaxScript(daxText);
                Assert.AreEqual(bim.Measures.Count, measuresFromDax.Measures.Count);

                var expected = input;
                var actual = measuresFromDax.UpdateMeasuresInXmla(input);
                WindiffAssert.AreEqualNormalizedXmla(expected, actual);
                Assert.Fail("Exception expected");
            }
            catch (Exception e)
            {
                StringAssert.Contains(e.Message, "KPI are not yet supported");
            }
        }

        [TestMethod]
        public void M3()
        {
            Assert.Inconclusive("Disabled, KPI are NOT yet supported");
            string input = Utils.ReadFileFromResources("M3.bim");
            var bim = MeasuresContainer.ParseXmlaAsText(input);
            Assert.IsNotNull(bim.Measures);
            Assert.AreEqual(88, bim.Measures.Count);
            var daxText = bim.GetDaxText();
            Assert.IsNotNull(daxText);

            var measuresFromDax = MeasuresContainer.ParseDaxScript(daxText);
            Assert.AreEqual(bim.Measures.Count, measuresFromDax.Measures.Count);

            var expected = input;
            var actual = measuresFromDax.UpdateMeasuresInXmla(input);
            WindiffAssert.AreEqualNormalizedXmla(expected, actual);
            Assert.Fail("Exception expected");
        }
    }
}
